﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EC_DataBase;

namespace EasyCheque.Person
{
    public partial class ChangePassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label_CommitMessage.Visible = false;
            Label_CommitMessage.Text = "";
        }

        protected void Button_ChangeLoginPassword_Click(object sender, EventArgs e)
        {
            // 检测密码正确性。
            if (!Security.MD5.Exam(TextBox_PASSWORD.Text, System.Text.Encoding.ASCII, OperateDB.GetLoginPwd(Convert.ToInt32(Session["uid"]))))
            {
                // 检测到密码无效。
                System.Diagnostics.Debug.WriteLine("Password invalid.");
                Label_CommitMessage.Visible = true;
                Label_CommitMessage.CssClass = "tipwrong";
                Label_CommitMessage.Text = "原密码无效。";
                return;
            }
            else
            {
                Label_CommitMessage.Visible = false;
                Label_CommitMessage.Text = "";
            }

            // 开始向数据库写新密码。
            if (OperateDB.ChangeLoginPwd(Convert.ToInt32(Session["uid"]), Security.MD5.HexCompute(TextBox_NewPassword.Text, System.Text.Encoding.ASCII)))
            {
                // 修改密码成功。

                TextBox_PASSWORD.Text = "";
                TextBox_NewPassword.Text = "";
                TextBox_ReinputPassword.Text = "";

                Label_CommitMessage.Visible = true;
                Label_CommitMessage.CssClass = "tipok";
                Label_CommitMessage.Text = "密码修改成功。";
                
                // Wander: 这里其实不用跳转。
            }
            else
            {
                Label_CommitMessage.Visible = true;
                Label_CommitMessage.CssClass = "tipwrong";
                Label_CommitMessage.Text = "密码修改失败。";
            }
        }
    }
}
